home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
optivc32
/
newcplx.h
< prev
next >
Wrap
C/C++ Source or Header
|
1999-03-06
|
63KB
|
1,286 lines
/* newcmplx.h
Include-File for the CMATH Complex Number Library
replaces <complex.h>.
Copyright (C) 1996-1999 Martin Sander
Address of the author:
Dr. Martin Sander Software Dev.
Sertuernerstr. 11
D-37085 Goettingen
Germany
MartinSander@Bigfoot.com
http://www.optivec.com
for C++, the following classes are defined:
a) if you choose the "classic" Borland C++ style: class complex;
b) otherwise:
classes complex<float>, complex<double>, and complex<long double>.
fComplex, dComplex, and eComplex are defined as synonyms for these classes.
Fo plain C, use <cmath.h> instead, which declares fComplex, dComplex,
and eComplex as structs, along with the same range of functions as
present in the complex C++ classes.
The classes complex and complex<double> are binary compatible with
the C struct dComplex. Similarly, complex<float> and struct fComplex
as well as class complex<long double> and struct eComplex are mutually
compatible. This is important if one has programs with some modules
written in C, others in C++.
All mathematical complex functions are implemented as a library
written in Assembler language. In comparison to C++ inline functions,
this leads to greater precision, greater speed and best security due
to complete error handling via the standard C error handling functions
_matherr() (for complex<float> and complex<double>)
and _matherrl() (for complex<long double>).
The declarations for the Standard Library complex classes have
partly been adapted from the implementation by Rogue Wave Software, Inc.
The function bodies, however, are completely new.
Note the following important differences between this implementation
and the one contained in <complex.h>:
- The real and imaginary parts are declared as public and are referred
to as Re and Im, so that you may always access them as z.Re and z.Im
in addition to the member functions real(z) and imag(z);
- The argument of all mathematical functions is a value, not a reference.
- The following functions and operators have been added:
friend complex cubic(complex); // third power
friend complex inv(complex); // 1.0 / z
friend complex ipow(complex __base, int __expo); // integer power
friend complex log2(complex);
friend complex powReExpo(complex __base, double __expoRe);
// explicit power with real exponent
friend complex powReBase(double __baseRe, complex __expo);
// explicit power of real base
friend complex quartic(complex); // fourth power
friend complex square(complex);
friend int operator==(complex &, double);
friend int operator!=(complex &, double);
Also new are many of the mixed-accuracy level binary operators.
*/
#ifndef __cplusplus
#error Must use C++ for complex classes. Include <cmath.h> for the plain-C version
#endif
#if !defined(__NEWCPLX_H)
#define __NEWCPLX_H
/* define _VFAR to get around the buggy definition of _FAR : */
#if defined __TINY || defined __SMALL__ || defined __MEDIUM__
#if defined(_RTLDLL) || defined(_CLASSDLL)
#error Must use static BC Runtime Library with OptiVec in models TINY, SMALL, MEDIUM
#endif
#define _VFAR near /* even in case of DS!=SS */
#elif defined __FLAT__ || defined _WIN32
#define _VFAR
#else
#define _VFAR far
#endif
#ifdef __BORLANDC__
#pragma option -a-
#if (__BORLANDC__ >= 0x450)
#define __cmf _RTLENTRY _EXPFUNC
#define __cmo _RTLENTRY
#else
#define __cmf _Cdecl _FARFUNC
#define __cmo _Cdecl
#endif
#if __BORLANDC__ < 0x500
#define VBOOL int
#else
#define VBOOL bool
#endif
#else /* Visual C++, Watcom */
#pragma pack( push,1 )
#define VBOOL int
#define __cmf _cdecl
#define __cmo _cdecl
#endif /* avoid insertion of dummy bytes */
#define _VFARC const _VFAR
#if !defined(__IOSTREAM_H)
#include <iostream.h>
#endif
#if defined( CMATH_CLASSIC_COMPLEX )
// classic Borland C++ class complex only. This has double precision.
// complex numbers of float and of long double precision are
// implemented as structs. So there is no constructor! Moreover, there
// are no functions defined for the float and long double complex numbers.
#if !defined(__IOSTREAM_H)
#include <iostream.h>
#endif
#if !defined(RC_INVOKED)
#if defined(__BCOPT__)
#if !defined(__FLAT__) && ((__BORLANDC__ < 0x450) || !defined(_RTL_ALLOW_po))
#pragma option -po- // disable Object data calling convention
#endif
#endif
#pragma option -Vo-
#if defined(__STDC__)
#pragma warn -nak
#endif
#endif /* !RC_INVOKED */
#if(__BORLANDC__ >= 0x450)
class _EXPCLASS complex {
#else
_CLASSDEF(complex)
class _CLASSTYPE complex {
#endif
public:
// constructors
complex( double Re_part, double Im_part ) {Re=Re_part; Im=Im_part;}
complex( double Re_part ) {Re=Re_part; Im=0;}
complex() {}; // more efficient to have these three separate variants
// Cartesian complex from polar coordinates:
friend complex __cmf polar(double _mag, double _angle=0);
// basic operations:
double real() { return Re; } // real part
double real(complex _VFARC &_z) { return _z.Re; }
double imag() { return Im; } // imaginary part
double imag(complex _VFARC &_z) { return _z.Im; } // imaginary part
friend complex __cmf neg(complex _VFARC &); // same as unary operator -
friend complex __cmf conj(complex _VFARC &); // complex conjugate
friend double __cmf norm(complex); // square of the magnitude
friend double __cmf arg(complex); // angle in the plane
// Unary operators
complex _VFAR & __cmo operator+();
friend complex _VFAR & __cmf operator-( complex _VFARC &);
// Binary operators
friend complex __cmf operator+(complex _VFARC &, complex _VFARC &);
friend complex __cmf operator+(double, complex _VFARC &);
friend complex __cmf operator+(complex _VFARC &, double);
friend complex __cmf operator-(complex _VFARC &, complex _VFARC &);
friend complex __cmf operator-(double, complex _VFARC &);
friend complex __cmf operator-(complex _VFARC &, double);
friend complex __cmf operator*(complex _VFARC &, complex _VFARC &);
friend complex __cmf operator*(complex _VFARC &, double);
friend complex __cmf operator*(double, complex _VFARC &);
friend complex __cmf operator/(complex _VFARC &, complex _VFARC &);
friend complex __cmf operator/(complex _VFARC &, double);
friend complex __cmf operator/(double, complex _VFARC &);
friend VBOOL __cmf operator==(complex _VFARC &, complex _VFARC &);
friend VBOOL __cmf operator==(complex _VFARC &, double);
friend VBOOL __cmf operator!=(complex _VFARC &, complex _VFARC &);
friend VBOOL __cmf operator!=(complex _VFARC &, double);
complex _VFAR & __cmo operator+=(complex _VFARC &);
complex _VFAR & __cmo operator+=(double);
complex _VFAR & __cmo operator-=(complex _VFARC &);
complex _VFAR & __cmo operator-=(double);
complex _VFAR & __cmo operator*=(complex _VFARC &);
complex _VFAR & __cmo operator*=(double);
complex _VFAR & __cmo operator/=(complex _VFARC &);
complex _VFAR & __cmo operator/=(double);
// Overloaded ANSI C math functions with error handling via matherr():
friend double __cmf abs(complex); // complex pointer magnitude
friend complex __cmf acos(complex);
friend complex __cmf asin(complex);
friend complex __cmf atan(complex);
friend complex __cmf cos(complex);
friend complex __cmf cosh(complex);
friend complex __cmf cubic(complex); // raise to the third power
friend complex _